package com.ruoyi.sms.service;


import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.sms.constant.Dh3tAccount;
import com.ruoyi.sms.constant.Dh3tSmsPath;
import com.ruoyi.sms.constant.SmsTemplate;
import com.ruoyi.sms.domain.bo.DwSmsBO;
import com.ruoyi.sms.domain.dto.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Xz
 * @date 2021-10-28 15:35
 */

@Service
public class Dh3tSendSmsServiceImpl {


    private static Dh3tAccount dh3tAccount = new Dh3tAccount();


    public DewinSendSmsResponse sendSms(DwSmsBO dwSmsBO) {
        SendWithTemplateReqDTO sendWithTemplateReqDTO = new SendWithTemplateReqDTO();
        // 构建请求体,账号密码手机号
        if (StringUtils.isNotEmpty(dwSmsBO.getBatch())) {
            sendWithTemplateReqDTO.setAccount(dh3tAccount.getMarketingAccount());
            sendWithTemplateReqDTO.setPassword(dh3tAccount.getMarketingPassword());
        } else {
            sendWithTemplateReqDTO.setAccount(dh3tAccount.getIndustryAccount());
            sendWithTemplateReqDTO.setPassword(dh3tAccount.getIndustryPassword());
        }
//        sendWithTemplateReqDTO.setSign(dwSmsBO.getSignName());
        sendWithTemplateReqDTO.setPhones(dwSmsBO.getPhones());
        if (!StringUtils.isEmpty(dwSmsBO.getTemplateCode())) {
            sendWithTemplateReqDTO.setTemplate(createTemplate(dwSmsBO.getTemplateParam(), dwSmsBO.getTemplateCode()));
        } else if (!StringUtils.isEmpty(dwSmsBO.getSms())) {
            sendWithTemplateReqDTO.setContent(dwSmsBO.getSms());
        } else {
            throw new ServiceException("短信内容为空");
        }

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpResponse response = HttpUtil
            .createPost(Dh3tSmsPath.HOST + Dh3tSmsPath.SEND_WITH_TEMPLATE)
            .header(headers)
            .body(JsonUtils.toJsonString(sendWithTemplateReqDTO)).execute();

        Dh3tSendSmsResponse dh3tSendSmsResponse = JsonUtils.parseObject(response.body(), Dh3tSendSmsResponse.class);

        return DewinSendSmsResponse.dh3tDewinSendSmsResponse(dh3tSendSmsResponse);
    }

    private Template createTemplate(String templateParam, String templateCode) {
        Template template = new Template();
        // 解析String
        LinkedHashMap<String, String> templateMaps = JsonUtils.parseObject(templateParam, LinkedHashMap.class);
      /*  if(SmsTemplate.dh3t.get(templateCode).size() != templateMaps.size()){
            throw new ServiceException("参数缺失");
        }*/
        // 构建模板参数  提醒您的运单尾号${0,4}的${0,10}包裹放在${0,30}，已经7天，请及时领取
        template.setId(templateCode);
        List<TemplateVariable> templateVariables = new ArrayList<>();
        for (Map.Entry<String, String> templateMap : templateMaps.entrySet()) {
            TemplateVariable templateVariableExpressNum = new TemplateVariable();
            // 哪个参数放哪个位置需要固定
            templateVariableExpressNum.setName(SmsTemplate.dh3t.get(templateCode).get(templateMap.getKey()));
            templateVariableExpressNum.setValue(templateMap.getValue());
            templateVariables.add(templateVariableExpressNum);
        }
        template.setVariables(templateVariables);
        return template;
    }
}

